package com.eeds.service.file.impl;


import com.eeds.common.model.RespEnum;
import com.eeds.common.table.FileCabinetEntity;
import com.eeds.dao.FileCabinetDao;
import com.eeds.date.DateUtil;
import com.eeds.exception.ServerException;
import com.eeds.model.file.req.ReqAddFileInfoVO;
import com.eeds.model.file.req.ReqDeleteFileInfoVO;
import com.eeds.model.file.req.ReqGetFileInfoVO;
import com.eeds.model.file.req.ReqUpdateFileInfoVO;
import com.eeds.model.file.res.ResGetFileVO;
import com.eeds.para.ParametersUtil;
import com.eeds.service.file.FileService;
import com.eeds.service.upload.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private FileCabinetDao fileCabinetDao;

    @Autowired
    private UploadService uploadService;

    /**
     * 获取文件列表
     *
     * @return
     */
    @Override
    public List<ResGetFileVO> getFileList() {
        return fileCabinetDao.getFileList();
    }

    /**
     * 获取文件信息
     *
     * @param reqGetFileInfoVO
     * @return
     */
    @Override
    public ResGetFileVO getFileInfo(ReqGetFileInfoVO reqGetFileInfoVO) {
        return fileCabinetDao.getFileInfo(reqGetFileInfoVO);
    }

    /**
     * 添加文件信息
     *
     * @param reqAddFileInfoVO
     * @return
     */
    @Override
    @Transactional
    public Integer addFileInfo(ReqAddFileInfoVO reqAddFileInfoVO) {
        if (reqAddFileInfoVO.getType() == 1) {
            if (ParametersUtil.isNull(reqAddFileInfoVO.getIdxSysFiles())) {
                throw new ServerException(RespEnum.REQUEST_PARAM_ERROR, "文件id不能为空");
            } else {
                uploadService.updateFileStatus(reqAddFileInfoVO.getIdxSysFiles());

            }
        }
        reqAddFileInfoVO.setIdxSysFiles(reqAddFileInfoVO.getIdxSysFiles() == null ? 0 : reqAddFileInfoVO.getIdxSysFiles());
        FileCabinetEntity fileCabinetEntity = new FileCabinetEntity();
        fileCabinetEntity.setParent(reqAddFileInfoVO.getParent());
        fileCabinetEntity.setIdxSysFiles(reqAddFileInfoVO.getIdxSysFiles());
        fileCabinetEntity.setName(reqAddFileInfoVO.getName());
        fileCabinetEntity.setSort(reqAddFileInfoVO.getSort());
        FileCabinetEntity fileCabinetParent = fileCabinetDao.selectById(reqAddFileInfoVO.getParent());
        if (ParametersUtil.isNull(fileCabinetParent)) {
            throw new ServerException(RespEnum.DIR_IS_NULL);
        }
        fileCabinetEntity.setLevel(fileCabinetParent.getLevel() + 1);
        fileCabinetEntity.setType(reqAddFileInfoVO.getType());
        fileCabinetEntity.setStatus(1);
        Date date = DateUtil.nowDate();
        fileCabinetEntity.setCreateTime(date);
        fileCabinetEntity.setUpdateTime(date);
        fileCabinetDao.insert(fileCabinetEntity);
        return 1;
    }

    /**
     * 修改文件信息
     *
     * @param reqUpdateFileInfoVO
     * @return
     */
    @Override
    public Integer updateFileInfo(ReqUpdateFileInfoVO reqUpdateFileInfoVO) {
        if (reqUpdateFileInfoVO.getType() == 1) {
            if (ParametersUtil.isNull(reqUpdateFileInfoVO.getIdxSysFiles())) {
                throw new ServerException(RespEnum.REQUEST_PARAM_ERROR, "文件id不能为空");
            } else {
                uploadService.updateFileStatus(reqUpdateFileInfoVO.getIdxSysFiles());
                reqUpdateFileInfoVO.setIdxSysFiles(reqUpdateFileInfoVO.getIdxSysFiles() == null ? 0 : reqUpdateFileInfoVO.getIdxSysFiles());
            }
        }
        FileCabinetEntity fileCabinetEntity = new FileCabinetEntity();
        fileCabinetEntity.setName(reqUpdateFileInfoVO.getName());
        fileCabinetEntity.setSort(reqUpdateFileInfoVO.getSort());
        fileCabinetEntity.setType(reqUpdateFileInfoVO.getType());
        fileCabinetEntity.setIdxSysFiles(reqUpdateFileInfoVO.getIdxSysFiles());
        fileCabinetEntity.setStatus(1);
        Date date = DateUtil.nowDate();
        fileCabinetEntity.setUpdateTime(date);
        fileCabinetEntity.setIdFileCabinet(reqUpdateFileInfoVO.getIdFileCabinet());
        fileCabinetDao.updateById(fileCabinetEntity);
        return 1;
    }

    /**
     * 删除文件信息
     *
     * @param reqDeleteFileInfoVO
     * @return
     */
    @Override
    public Integer deleteFileInfo(ReqDeleteFileInfoVO reqDeleteFileInfoVO) {
        FileCabinetEntity fileCabinetEntity = new FileCabinetEntity();
        fileCabinetEntity.setIdFileCabinet(reqDeleteFileInfoVO.getIdFileCabinet());
        fileCabinetEntity.setStatus(0);
        return fileCabinetDao.updateById(fileCabinetEntity);
    }
}
